<script setup>
import { ref, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { addApi, updateApi, delApi } from '@/api/logistics/logisticsCompany'

const emit = defineEmits(['refresh'])
const ruleFormRef = ref(null)
const rules = ref({
  companyName: [
    { required: true, message: '请输入', trigger: 'blur' },
    { min: 3, max: 10, message: '长度应该在3-10', trigger: 'blur' },
  ],
  linkUser: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ],
  linkePhone: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ]
})
const dialogVisible = ref(false)
const dialogFormData = ref({
  id: null,
  companyName: '',
  minPrice: 1000,
  linkUser: '',
  linkePhone: '',
  sort: 1,
  saasId: null,
  isDel: 1
})

const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    id: null,
    companyName: '',
    minPrice: 1000,
    linkUser: '',
    linkePhone: '',
    sort: 1,
    saasId: null,
    isDel: 1
  }
  ruleFormRef.value.resetFields()
}
// const confirm = async () => {
//   let res = null
//   if (dialogFormData.value.id) {
//     res = await updateApi(dialogFormData.value)
//   } else {
//     res = await addApi(dialogFormData.value)
//   }
//   if (res) {
//     ElMessage.success('操作成功!')
//     resetForm()
//     emit('refresh')
//   } else {
//     ElMessage.error('操作失败！')
//   }
// }
const confirm = async () => {
  let res = null;
  if (!ruleFormRef) return
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (dialogFormData.value.id) {
        res = await updateApi(dialogFormData.value)
      } else {
        res = await addApi(dialogFormData.value)
      }
      if (res.code) {
        ElMessage.success('操作成功!')
        resetForm()
        instance.emit('search')
      } else {
        ElMessage.error('操作失败！')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

defineExpose({
  dialogFormData,
  dialogVisible
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="物流公司信息" width="500px">
    <el-form :model="dialogFormData" label-width="100px" ref="ruleFormRef" :rules="rules">
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="dialogFormData.companyName" placeholder="请输入公司名称" />
      </el-form-item>

      <el-form-item label="最低费用(分)">
        <el-input-number v-model="dialogFormData.minPrice" :min="0" />
      </el-form-item>

      <el-form-item label="联系人" prop="linkUser">
        <el-input v-model="dialogFormData.linkUser" placeholder="请输入联系人" />
      </el-form-item>

      <el-form-item label="联系电话" prop="linkePhone">
        <el-input v-model="dialogFormData.linkePhone" placeholder="请输入联系电话" />
      </el-form-item>

      <el-form-item label="排序">
        <el-input-number v-model="dialogFormData.sort" :min="1" />
      </el-form-item>

      <el-form-item label="是否有效">
        <el-switch v-model="dialogFormData.isDel" :active-value="1" :inactive-value="2" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>